package cn.didi.feidi.androidorder.util;

import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;

import cn.didi.feidi.androidorder.OrderApplication;
import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.assembly.CommomDialog;


/**
 * Created by king on 2017/9/2.
 */

public class NotificationUtil {
    private static int NOTIFICATION_ID_LIVE = 1;

    /**
     * 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
     * <uses-permission android:name="android.permission.VIBRATE" />
     * 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
     */
    //  待优化
    public static void showNotifyWithVibrate(final Context mContext, String title, String message, Class mClass) {
        if (NotificationManagerCompat.from(mContext).areNotificationsEnabled()){
            NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            //震动也有两种设置方法,与设置铃声一样,在此不再赘述
            long[] vibrate = new long[]{0, 500, 1000, 1500};
            NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(true)
                    //使用系统默认的震动参数,会与自定义的冲突
//                .setDefaults(Notification.DEFAULT_VIBRATE)
//                //声音
//                .setDefaults(Notification.DEFAULT_SOUND)
                    //自定义震动效果
//                .setVibrate(vibrate)
                    .setSmallIcon(R.drawable.app_logo);
//        另一种设置震动的方法
            Notification notify = builder.build();

            notify.defaults=Notification.DEFAULT_ALL;
            //调用自己设置的震动
            //notify.vibrate = vibrate;
            //mManager.notify(3,notify);

            Intent clickIntent = new Intent(mContext,mClass); //点击通知之后要发送的广播
            int id = (int) (System.currentTimeMillis() / 1000);
            PendingIntent contentIntent = PendingIntent.getActivity(mContext,id,clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//        notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
            notify.contentIntent=contentIntent;
            mNotificationManager.notify(NOTIFICATION_ID_LIVE, notify);
            NOTIFICATION_ID_LIVE++;
        }else {
            if (OrderApplication.getCurActivity()!=null&& AppUtil.isBackground(mContext)){
                new CommomDialog(OrderApplication.getCurActivity(), R.style.dialog, "开启应用通知？", new CommomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if (confirm) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BASE) {
                                // 进入设置系统应用权限界面
                                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                                mContext.startActivity(intent);
                            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {// 运行系统在5.x环境使用
                                // 进入设置系统应用权限界面
                                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                                mContext.startActivity(intent);
                            }
                        } else {
                            dialog.dismiss();
                        }
                    }
                }, false, true).setNegativeButton("取消").setPositiveButton("设置").show();
            }
        }
    }
}
